package com.example.config;

import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author yinxin
 * @version 1.0
 * @Description: 消息队列配置
 * 发送 关单消息=>延迟exchange=》order.close.delay.queue->死信exchange->order.close.queue
 * @date 2022/6/109:51
 */
@Data
@Configuration
public class RabbitMQConfig {
    /**
     * 交换机
     */
    private String orderEventExchange = "order.event.exchange";

    /**
     * 延迟队列，不能被消费者监听 时间结束到死信队列
     */
    private String orderCloseDelayQueue = "order.close.delay.queue";

    /**
     * 关闭订单的队列，延迟队列消息过期后转发队列，用于被消费者监听
     */
    private String orderCloseQueue = "order.close.queue";

    /**
     * 进入到延迟队列的routingKey
     */
    private String orderCloseDelayRoutingKey = "order.close.delay.routing.key";

    /**
     * 进入死信队列的routingKey
     */
    private String orderCloseRoutingKey = "order.close.routing.key";

    /**
     * 过期时间，毫秒单位，零时改为2分钟过期
     */
    private Integer ttl = 1000 * 120;

    /**
     * 消息转换器
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 交换机topic类型,一个业务一个交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(orderEventExchange, true, false);
    }


    /**
     * 一段时间过期的Queue,没有消费者
     * @return
     */
    @Bean
    public Queue orderCloseDelayQueue(){
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",orderEventExchange);
        args.put("x-dead-letter-routing-key",orderCloseRoutingKey);
        args.put("x-message-ttl",ttl);
        return new Queue(orderCloseDelayQueue,true,false,false,args);
    }

    /**
     * 死信队列，关闭订单队列
     * @return
     */
    @Bean
    public Queue orderCloseQueue(){
        //队列名、是否持续化、是否独占、是否自动删除
        return new Queue(orderCloseQueue,true,false,false);
    }

    /**
     * 绑定订单延迟队列和订单交换机
     * @return
     */
    @Bean
    public Binding orderCloseDelayBinding(){
        return new Binding(orderCloseDelayQueue, Binding.DestinationType.QUEUE
                ,orderEventExchange,orderCloseDelayRoutingKey,null);
    }

    /**
     * 绑定订单关闭队列（死信队列）到死信交换机
     * @return
     */
    @Bean
    public Binding orderCloseBinding(){
        return new Binding(orderCloseQueue, Binding.DestinationType.QUEUE
                ,orderEventExchange,orderCloseRoutingKey,null);
    }

    //==============订单支付成功配置===============
    /**
     * 更新订单 队列
     */
    private String orderUpdateQueue = "order.update.queue";

    /**
     * 订单发放流量包 队列
     */
    private String orderTrafficQueue = "order.traffic.queue";

    /**
     * 微信回调发送通知的routing Key【发送消息用】
     */
    private String orderUpdateTrafficRoutingKey = "order.update.traffic.routing.key";

    /**
     * topic用于绑定订单队列和交换机的
     */
    private String orderUpdateBindingKey = "order.update.*.routing.key";

    /**
     * topic用于绑定发流量队列和交换机的
     */
    private String orderTrafficBindingKey = "order.*.traffic.routing.key";

    @Bean
    public Binding orderUpdateBinding(){
        return new Binding(orderUpdateQueue,Binding.DestinationType.QUEUE,orderEventExchange,orderUpdateBindingKey,null);
    }

    @Bean
    public Queue orderUpdateQueue(){
        return new Queue(orderUpdateQueue,true,false,false);
    }

    @Bean
    public Binding orderTrafficBinding(){
        return new Binding(orderTrafficQueue,Binding.DestinationType.QUEUE,orderEventExchange,orderTrafficBindingKey,null);
    }

    @Bean
    public Queue orderTrafficQueue(){
        return new Queue(orderTrafficQueue,true,false,false);
    }

}
